package com.rimi.lzhg.service.impl;

import com.rimi.lzhg.dao.MenuMapper;
import com.rimi.lzhg.dao.OrderMapper;
import com.rimi.lzhg.entry.Menu;
import com.rimi.lzhg.entry.Order;
import com.rimi.lzhg.entry.OrderData;
import com.rimi.lzhg.entry.OrderExample;
import com.rimi.lzhg.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public boolean saveOrder(Order order) {
        if(order==null){
            return false;
        }
        order.setCreateTime(new Date());
        //默认未取消订单
        if(order.getIsCancel()==null){
            order.setIsCancel(0);
        }
        //默认菜单类型 午餐
        if(order.getType()==null){
            order.setType(1);
        }
        //通过用户ID和午餐、晚餐类型 查询该订单是否存在
        Order order1 = orderMapper.getOrderByUserIdAndType(order);
        //如果未添加过
        if(order1==null){
            int insert = orderMapper.insertSelective(order);
            if(insert>0){
                int i = orderMapper.insertOrderMenu(order);
                if(i>0){
                    return true;
                }
            }
        }else{
            //用户上传的订单
            List<OrderData> orders = order.getOrders();
            System.err.println("用户输入的数据"+orders);
            //将集合长度进行记录，避免在删除元素时影响集合长度的判断
            int size=orders.size();
            int count=1;
            Iterator<OrderData> iterator = orders.iterator();
            while (iterator.hasNext()){
                OrderData orderData = iterator.next();
                //菜品ID
                Integer id = orderData.getId();
                //该订单对应的所有菜单
                List<Menu> menuList = order1.getMenuList();
                for (Menu menu : menuList) {
                    //如果已经添加过的菜品
                    if(menu.getId()==id){
                        System.out.println("修改次数"+(count++));
                        //用户选择的数量
                        Integer nums = orderData.getNums();
                        //订单原数量
                        boolean b = orderMapper.updateOrderMenu(order1.getId(),id, nums);
                        if (b){
                            //如果用户只添加一项重复菜品
                            if(size==1){
                                return true;
                            }
                            //将重复的菜品从集合中移除，避免重复添加订单菜品
                            iterator.remove();
                        }
                        break;
                    }
                }
            }
            //集合中存在数据，但在前面已经全部处理过了
            if(size>0&&orders.size()==0){
                return true;
            }
            //添加
            order1.setOrders(orders);
            for (OrderData orderData : orders) {
                System.err.println("最终数据："+orderData);
            }
            int i = orderMapper.insertOrderMenu(order1);
            if(i>0){
                return true;
            }
        }
        return false;
    }

    @Override
    public List<Order> getOrderList(Order order) {
        if(order==null){
            return orderMapper.selectByExample(null);
        }
        //条件查询
        OrderExample orderExample=new OrderExample();
        OrderExample.Criteria criteria = orderExample.createCriteria();
        /* 用户ID */
        if(order.getUserId()!=null){
            criteria.andUserIdEqualTo(order.getUserId());
        }
        //查询菜单对应的数据
        List<Order> orders = orderMapper.selectByExample(orderExample);
        for (Order order1 : orders) {
            List<Menu> menuList = order1.getMenuList();
            for (Menu menu : menuList) {
                System.out.println(order1.getType());
                if(2==order1.getType()){
                    int i = menuMapper.countMenuByUserId(order1, menu.getId());
                    System.out.println("计算结果"+i+"-----"+order1.getType()+"---"+menu.getId());
                    menu.setDinnerNumber(i);
                }else{
                    menu.setLunchNumber(menuMapper.countMenuByUserId(order1,menu.getId()));
                    System.out.println("进入这边");
                }
            }
        }
        return orders;
    }

    @Override
    public Order getOrderByUserId(Integer userId,Date time) {
        OrderExample orderExample=new OrderExample();
        if(userId!=null||time!=null){
            OrderExample.Criteria criteria = orderExample.createCriteria();
            if(userId!=null){
                criteria.andUserIdEqualTo(userId);
            }
            if (time!=null){
                criteria.andCreateTimeEqualTo(time);
            }else{
                criteria.andCreateTimeEqualTo(new Date());
            }
        }
        List<Order> orders = orderMapper.selectByExample(orderExample);
        if (orders.size()>0){
            return orders.get(0);
        }
        return null;
    }
}
